#!/bin/bash
# 所有压缩包的位置
packages_dir=/usr/local/src
app_install_dir=/usr/local

# mariadb 数据位置
mariadb_data=/data/mysql
# 安装位置
mariadb_local=/usr/local/mysql

# 安装依赖
install_depend() {
  echo "安装所需的依赖...."
  yum install -y apr* autoconf automake bison bzip2 cloog-ppl compat* cpp curl curl-devel fontconfig fontconfig-devel freetype freetype* freetype-devel gcc gcc-c++ gtk+-devel gd gettext gettext-devel glibc kernel kernel-headers keyutils keyutils-libs-devel krb5-devel libcom_err-devel libpng libpng-devel libjpeg* libsepol-devel libselinux-devel libstdc++-devel libtool* libgomp libxml2 libxml2-devel libXpm* libtiff libtiff* make mpfr ncurses* ntp openssl openssl-devel patch pcre-devel perl php-common php-gd policycoreutils telnet t1lib t1lib* nasm nasm* zlib-devel gd-devel
  if [ $? -eq 0 ]; then
    echo " 安装：${1}成功"
  else
    echo "  安装：${1}失败"
  fi
}

# 添加www用户和组添加 mysql和组
# add_user 用户 组
add_user() {
  groupadd "${2}" && useradd -g "${1}" "${1}" -s /bin/false
  echo "添加了用户:${1}和用户组:${2}"
}

# 批量解压所有压缩包
unzip_packages() {
  cd "${1}"
  for package in ls *tar.gz*; do
    tar -zvxf $package
    make_app_dir $package
  done

  for package in ls *tar.bz2*; do
    tar -jvxf $package
    make_app_dir $package
  done
}

# 批量创建安装的目录
make_app_dir() {
  if [[ ${1} == *-* ]]; then
    # 获取以-分割的单词
    mkdir -p ${app_install_dir}/${package%%-*}
  else
    mkdir -p ${app_install_dir}/${package%%.*}
  fi
}

check_res() {
  if [ $? -eq 0 ]; then
    echo " 安装：${1}成功"
  else
    echo "  安装：${1}失败"
    exit 0
  fi
}

# 安装的应用不需要指定路径的
install_default() {
  for app_dir in ${*}; do
    cd $packages_dir/${app_dir}* && ./configure && make && make install
  done
}

install_by_prefix() {
  echo "安装：${1}"
  cd $packages_dir/${1}* && chmod +x configure &&  ./configure ${2} && make && make install
  check_res ${1}
}

install_openssl() {
  echo "安装：${1}"
  cd $packages_dir/${1}* && ./config ${2} && make && make install
  check_res ${1}
}




# 安装数据库
install_mariadb() {
  mkdir -p ${mariadb_data} && chown -R mysql:mysql ${mariadb_data} && mkdir -p ${mariadb_local} &&
    cd $packages_dir/${1}* && cmake . -DCMAKE_INSTALL_PREFIX=${mariadb_local} -DMYSQL_DATADIR=${mariadb_data} -DSYSCONFDIR=/etc && make && make install
}

# 初始化数据库
init_mariadb() {
  mv /etc/my.cnf /etc/my.cnf.bak
  cd ${mariadb_local} &&
    ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql &&
    ln -s /usr/local/mysql/my.cnf /etc/my.cnf &&
    cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld &&
    chmod /etc/init.d/mysqld &&
    chkconfig mysqld on &&
    sed -i "46cbasedir=${mariadb_local}" /etc/rc.d/init.d/mysqld &&
    sed -i "47cdatadir=${mariadb_data}" /etc/rc.d/init.d/mysqld &&
    source /etc/profile &&
    ln -s ${mariadb_local}/lib/mysql /usr/lib/mysql &&
    ln -s ${mariadb_local}/include/mysql /usr/include/mysql &&
    mkdir /var/lib/mysql &&
    ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock
}

install_php() {
  cd $packages_dir/php* &&
    export LD_LIBRARY_PATH=/usr/local/libgd/lib &&
    ./configure --prefix=/usr/local/php \
      --with-config-file-path=/usr/local/php/etc \
      --with-mysql=/usr/local/mysql \
      --with-mysqli=/usr/local/mysql/bin/mysql_config \
      --with-mysql-sock=/tmp/mysql.sock \
      --with-pdo-mysql=/usr/local/mysql \
      --with-gd \
      --with-png-dir=/usr/local/libpng \
      --with-jpeg-dir=/usr/local/jpegsrc \
      --with-freetype-dir=/usr/local/freetype \
      --with-xpm-dir=/usr/ \
      --with-vpx-dir=/usr/local/libvpx/ \
      --with-zlib-dir=/usr/local/zlib \
      --with-t1lib=/usr/local/t1lib \
      --with-iconv \
      --enable-libxml \
      --enable-xml \
      --enable-bcmath \
      --enable-shmop \
      --enable-sysvsem \
      --enable-inline-optimization \
      --enable-opcache \
      --enable-mbregex \
      --enable-fpm \
      --enable-mbstring \
      --enable-ftp \
      --enable-gd-native-ttf \
      --with-openssl \
      --enable-pcntl \
      --enable-sockets \
      --with-xmlrpc \
      --enable-zip \
      --enable-soap \
      --without-pear \
      --with-gettext \
      --enable-session \
      --with-mcrypt \
      --with-curl \
      --enable-exif \
      --enable-ctype &&
    make && make install
}

add_path() {
  echo 'export PATH=$PATH:/usr/local/openssl/bin' >>/etc/profile &&
    echo 'export PATH=$PATH:/usr/local/mysql/bin' >>/etc/profile &&
    echo 'export PATH=$PATH:/usr/local/nginx/sbin' >>/etc/profile &&
    echo 'export PATH=$PATH:/usr/local/php/bin' >>/etc/profile &&
    source /etc/profile
}

# 安装依赖
#install_depend
#
## 创建用户和用户组
#add_user www www
#add_user mysql mysql
#
#
## 解压所有安装包并创建目录
#unzip_packages $packages_dir

# 安装的应用不需要指定路径的包
#install_default cmake yasm libmcrypt

# 安装 pcre
install_by_prefix pcre "--prefix=/usr/local/pcre"
# 安装OpenSSL
install_openssl openssl "--prefix=/usr/local/openssl"
install_by_prefix zlib "--prefix=/usr/local/zlib"
install_by_prefix libvpx "--as=yasm  --prefix=/usr/local/libvpx --enable-shared --enable-vp9"
install_by_prefix tiff "--prefix=/usr/local/tiff --enable-shared"
install_by_prefix libpng "--prefix=/usr/local/libpng --enable-shared --with-x=no"
install_by_prefix freetype "--prefix=/usr/local/freetype --enable-shared --with-x=no"
install_by_prefix jpeg "--prefix=/usr/local/jpegsrc --enable-shared"
install_by_prefix libgd "--prefix=/usr/local/libgd --enable-shared --with-jpeg=/usr/local/jpegsrc --with-png=/usr/local/libpng --with-freetype=/usr/local/freetype --with-fontconfig=/usr/local/freetype --with-xpm=/usr/ --with-tiff=/usr/local/tiff --with-vpx=/usr/local/libvpx"
install_by_prefix t1lib "--prefix=/usr/local/t1lib --enable-shared"
install_by_prefix nginx "--prefix=/usr/local/nginx --without-http_memcached_module --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-openssl=/usr/local/src/openssl --with-zlib=/usr/local/src/zlib --with-pcre=/usr/local/src/pcre --with-http_stub_status_module --with-http_sub_module"

##  安装数据库
#install_mariadb mariadb
#
#add_path
